package net.cyl.controller;

import com.alibaba.cloud.commons.lang.StringUtils;
import jodd.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import net.cyl.annotation.RepeatSubmit;
import net.cyl.constant.RedisKey;
import net.cyl.controller.request.ConfirmOrderRequest;
import net.cyl.controller.request.ProductOrderPageRequest;
import net.cyl.emums.BizCodeEnum;
import net.cyl.emums.ClinentTypeEnum;
import net.cyl.emums.ProductOrderPayTypeEnum;
import net.cyl.interceptor.LoginInterceptor;
import net.cyl.service.ProductOrderService;
import net.cyl.service.ProductService;
import net.cyl.util.CommonUtil;
import net.cyl.util.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
@RequestMapping("/api/productOrder/v1")
public class ProductOrderController {
    @Autowired
    private ProductOrderService productOrderService;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private ProductService productService;
    @PostMapping("/page")

    public JsonData  page(@RequestBody ProductOrderPageRequest request) {
        Map<String, Object> result= productOrderService.page(request);
        return JsonData.buildSuccess(result);

    }

    @GetMapping("query_state")
    public JsonData queryState(@RequestParam(value = "out_trade_no") String outTradeNo) {
       String state = productOrderService.queryProductOrderState(outTradeNo);
       return StringUtils.isEmpty(state)? JsonData.buildResult(BizCodeEnum.ORDER_CONFIRM_NOT_EXIST) : JsonData.buildSuccess(state);
    }

    @PostMapping("confirm")
    //@RepeatSubmit(limitType = RepeatSubmit.Type.TOKEN)
    public void confirmOrder(@RequestBody ConfirmOrderRequest orderRequest, HttpServletResponse response) {

        JsonData jsonData = productOrderService.confirmOrder(orderRequest);

        if (jsonData.getCode() == 0) {

            //端类型
            String client = orderRequest.getClientType();
            //支付类型
            String payType = orderRequest.getPayType();

            //如果是支付宝支付，跳转网页，sdk除非
            if (payType.equalsIgnoreCase(ProductOrderPayTypeEnum.ALIPAY.name())) {

                if (client.equalsIgnoreCase(ClinentTypeEnum.PC.name())) {

                    CommonUtil.sendHtmlMessage(response, jsonData);

                } else if (client.equalsIgnoreCase( ClinentTypeEnum.APP.name())) {

                } else if (client.equalsIgnoreCase( ClinentTypeEnum.H5.name())) {

                }

            } else if (payType.equalsIgnoreCase(ProductOrderPayTypeEnum.WEICHATPAY.name())) {
                //微信支付
                CommonUtil.sendJsonMessage(response, jsonData);
            }

        } else {
            log.error("创建订单失败{}", jsonData.toString());
            CommonUtil.sendJsonMessage(response, jsonData);
        }

    }
    /**
     * 下单前获取令牌防止重复提交
     *
     * @return
     */
    @GetMapping("token")
    public JsonData token() {
        long accountNo = LoginInterceptor.loginUserThreadLocal.get().getAccountNo();

        String token= CommonUtil.getStringNumRandom(32);
        String key = String.format(RedisKey.SUBMIT_ORDER_TOKEN_KEY,accountNo,token);
        //有效时间是30分钟
        redisTemplate.opsForValue().set(key, Thread.currentThread().getId()+"",30, TimeUnit.MINUTES);
        return JsonData.buildSuccess(token);
    }

}
